home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Shareware Grab Bag
/
Shareware Grab Bag.iso
/
090
/
cljune86.arc
/
STOOLS2.LTG
< prev
next >
Wrap
Text File
|
1986-03-31
|
7KB
|
193 lines
Listing 2
#include "stdio.h"
#include "dos.h"
#define BUF_SIZE 512
char buf[2 * BUF_SIZE], /*------- Input text file buffer -------*/
s_buf[BUF_SIZE];
/******************************************************************************/
/*----------------------------- The BUMP utility -----------------------------*/
/******************************************************************************/
/* */
/* Name: bump Version: 1.3 */
/* */è/* Last Edit: 03/21/86 Compiler: Lattice C, v2.15d */
/* */
/* Purpose: Take a passed name with embedded number and add one to the */
/* number. Search specified files for references to original */
/* name and change to references to new name, (with bumped number).*/
/* */
/******************************************************************************/
/*------------------ Copyright 1985 & 1986, by Gary Elfring ------------------*/
main(argc,argv)
int argc;
char *argv[];
{
extern FILE *fopen();
extern char *stpchr();
extern long ftell();
extern char *calloc();
FILE *in_file;
char *t,*s,*data,*old;
char file_old[80],
file_new[80];
char *p,
ext[4],
b_name[9], /*------ Part of file name before embedded number ------*/
e_name[9], /*------- Part of file name after embeded number -------*/
num[9], /*--------- ASCII number embeded in file name ----------*/
control[10];
int i, j, /*------------------- Work counters --------------------*/
length,
cnt, /*------------ Count of number char in name ------------*/
position, /*--------- Flag to tell where in name we are ----------*/
bytes_read,
found;
unsigned file_point;
long pos;
/*----- Give them some help if they don't know how to invoke the program -----*/
if (argc < 3)
{
printf("BUMP searches a file or series of files for a specific file name.\n");
printf("This name must end in a number. Anytime the file name is found the\n");
printf("number portion will be increased by one.\n\n");
printf("USE --> bump file1 file2 file3 ... fileN nameXX.ext{cr}\n");
printf(" where file1-N are files to search\n");
printf(" and nameXX.ext is any file name with the name part ending\n");
printf(" in a 1 to 4 digit number.\n");
exit(1);
}
/*-------------------------- Get file name to bump ---------------------------*/
strcpy(file_old,argv[argc - 1]);è
/*--------------------------- Rip name into parts ----------------------------*/
/*----------------------- Fool with extension if found -----------------------*/
if ((p = stpchr(file_old,'.')) != NULL)
{
*p++ = 0;
strcpy(ext,p);
}
else
ext[0] = 0;
/*------------------------ Need length of whats left -------------------------*/
length = strlen(file_old);
/******************************************************************************/
/*-------------- Now rip whats left into numer and other parts ---------------*/
/******************************************************************************/
b_name[0] = e_name[0] = num[0] = cnt = 0;
position = 0;
for (i = 0, t = &file_old[0], p = &b_name[0]; i < length; ++i)
{
/*-------------- If we havent found any numbers in name yet --------------*/
if (position == 0)
{
/*------------------- Is this the first digit found --------------------*/
if (isdigit(*t))
{
*p = 0; /*-------------- End the beginning string --------------*/
++cnt;
++position;
p = &num[0]; /*------ Shift the pointer to the number storage -------*/
}
}
/*------------- Else if we already found a digit in the past -------------*/
else if (position == 1)
{
/*--------------------- See if still doing digits ----------------------*/
if (isdigit(*t))
++cnt;
else /*--------- If we just stopped finding digits, switch ----------*/
{
*p = 0;
++position;
p = &e_name[0]; /*----------- Move poiter to end storage ------------*/
}
}
*p++ = *t++; /*------------ ALWAYS save character somewhere -------------*/
}
*p = 0;
strcpy(file_old,argv[argc - 1]);
/*---- We need to build a format control string with # of digits in it -----*/
sprintf(control,"%%s%%0%dd%%s",cnt);
/*-------------------- Now build the "bumped" file name --------------------*/
sprintf(file_new,control,b_name,atoi(num) + 1,e_name);è if (ext[0] != 0)
{
strcat(file_new,".");
strcat(file_new,ext);
}
printf("Changing all reference to %s to %s in files:\n",file_old,file_new);
for (i = 1; i < (argc - 1); ++i)
printf("%s ",argv[i]);
printf("\n");
/*---------------------- While there are files to bump -----------------------*/
for (i = 1; i < (argc - 1); ++i)
{
found = 0;
/*----------------- Make sure the file to search exists ------------------*/
if ((in_file = fopen(argv[i],"rb+")) == NULL)
{
printf("Error: can't open file %s\n",argv[i]);
exit(1);
}
/*------------------ File size must be less than 64000 -------------------*/
fseek(in_file,0L,2);
if ((pos = ftell(in_file)) > 64000L)
{
fclose(in_file);
printf("Sorry, can not work with files larger than 64000 bytes\n");
exit(1);
}
fseek(in_file,0L,0);
/*----------------- Allocate the storage for search area -----------------*/
if ((data = calloc((unsigned)pos,1)) == NULL)
{
fclose(in_file);
printf("Sorry, can not allocate enough memory\n");
exit(1);
}
/*------------------------- Read in entire file --------------------------*/
bytes_read = fread(data,1,(unsigned)pos,in_file);
/*--------------- Look through entire file for all matches ---------------*/
for (file_point = 0; file_point < bytes_read - length; ++file_point)
{
t = old = &data[file_point];
s = &file_old[0];
for ( ; *t == *s; ++t)
{
if(!*++s)
{
/*------------------------- Found a match! -------------------------*/
found = !0;
for (j = 0; j < length; ++j)
*old++ = file_new[j]; /*------- Replace with new name -------*/è }
} /*---------------------- End for char = char ----------------------*/
} /*------------------------- End the search --------------------------*/
if (!found)
printf("Could not find %s in file %s\n",file_old,argv[i]);
else
{
fseek(in_file,0L,0);
fwrite(data,bytes_read,1,in_file);
}
fclose(in_file);
free(data);
} /*---------------------- End while text to read -----------------------*/
}